package com.hmall.common.config;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 给RabbitMQ配置消息转换器，改为JSON格式
 * 被其他微服务引入：得加入spring.factories，通过自动装配原理被扫描到
 */
@Configuration
@ConditionalOnClass(RabbitTemplate.class) //只有其它地方用到了RabbitTemplate，这个MqConfig才会生效
public class MqConfig {
    @Bean
    public MessageConverter messageConverter(){ //MessageConverter必须是amqp包下
        return new Jackson2JsonMessageConverter();
    }
}
